{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "F7C8x9_X0Bqs"
      },
      "outputs": [],
      "source": [
        "!pip install evaluate"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install bert_score"
      ],
      "metadata": {
        "id": "i4Z0YVal0Fvy"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from evaluate import load\n",
        "import csv\n",
        "\n",
        "input_file = 'output_ranking.csv'\n",
        "output_file = 'output_bert_score.csv'\n",
        "\n",
        "bertscore = load(\"bertscore\")\n",
        "\n",
        "with open(input_file,'r',newline=\"\") as f_input, open(output_file, 'a+', newline='') as f_output:\n",
        "  reader = csv.reader(f_input)\n",
        "  writer = csv.writer(f_output)\n",
        "\n",
        "  header = next(reader)\n",
        "  header.extend([\"output1_precision\",\"output1_recall\",\"output1_f1\",\"output2_precision\",\"output2_recall\",\"output2_f1\",\n",
        "                 \"output3_precision\",\"output3_recall\",\"output3_f1\",\"output4_precision\",\"output4_recall\",\"output4_f1\",\n",
        "                 \"output5_precision\",\"output5_recall\",\"output5_f1\",\"output6_precision\",\"output6_recall\",\"output6_f1\"])\n",
        "  writer.writerow(header)\n",
        "\n",
        "  for row in reader:\n",
        "\n",
        "    output1_text = [row[2].strip(\"\\n\")]\n",
        "    output2_text = [row[5].strip(\"\\n\")]\n",
        "    output3_text = [row[6].strip(\"\\n\")]\n",
        "    output4_text = [row[7].strip(\"\\n\")]\n",
        "    output5_text = [row[8].strip(\"\\n\")]\n",
        "    output6_text = [row[9].strip(\"\\n\")]\n",
        "\n",
        "\n",
        "    references = [row[1].strip(\"\\n\")]\n",
        "\n",
        "    results1 = bertscore.compute(predictions=output1_text, references=references, model_type=\"distilbert-base-uncased\")\n",
        "    results2 = bertscore.compute(predictions=output2_text, references=references, model_type=\"distilbert-base-uncased\")\n",
        "    results3 = bertscore.compute(predictions=output3_text, references=references, model_type=\"distilbert-base-uncased\")\n",
        "    results4 = bertscore.compute(predictions=output4_text, references=references, model_type=\"distilbert-base-uncased\")\n",
        "    results5 = bertscore.compute(predictions=output5_text, references=references, model_type=\"distilbert-base-uncased\")\n",
        "    results6 = bertscore.compute(predictions=output6_text, references=references, model_type=\"distilbert-base-uncased\")\n",
        "\n",
        "    print(results1[\"precision\"][0],results1[\"recall\"][0],results1[\"f1\"][0])\n",
        "\n",
        "    row.extend([results1[\"precision\"][0],results1[\"recall\"][0],results1[\"f1\"][0],\n",
        "                results2[\"precision\"][0],results2[\"recall\"][0],results2[\"f1\"][0],\n",
        "                results3[\"precision\"][0],results3[\"recall\"][0],results3[\"f1\"][0],\n",
        "                results4[\"precision\"][0],results4[\"recall\"][0],results4[\"f1\"][0],\n",
        "                results5[\"precision\"][0],results5[\"recall\"][0],results5[\"f1\"][0],\n",
        "                results6[\"precision\"][0],results6[\"recall\"][0],results6[\"f1\"][0]])\n",
        "    writer.writerow(row)\n"
      ],
      "metadata": {
        "id": "JJ9mo7f40HDq"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}